package com.ncr.travel.appmgr.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.ncr.travel.appmgr.model.shared.Graphics;
import com.ncr.travel.appmgr.model.shared.Product;
import com.ncr.travel.appmgr.model.shared.ProductCategory;
import com.ncr.travel.appmgr.model.shared.Rule;
import com.ncr.travel.appmgr.model.shared.Theme;
import com.ncr.travel.appmgr.service.ApplicationManagerService;
import com.ncr.travel.appmgr.service.model.ProductCategoriesResponse;
import com.ncr.travel.appmgr.service.model.ProductsResponse;
import com.ncr.travel.appmgr.service.model.ScreenConfiguration;
import com.ncr.travel.appmgr.service.provider.GraphicsProvider;
import com.ncr.travel.appmgr.service.provider.ProductsProvider;
import com.ncr.travel.appmgr.service.provider.RulesProvider;
import com.ncr.travel.appmgr.service.provider.ScreenConfigurationProvider;
import com.ncr.travel.appmgr.service.provider.ThemeProvider;

/**
 * @author as185325
 *
 */
@Component
@Transactional(readOnly = true)
public class ApplicationManagerServiceImpl implements ApplicationManagerService {
	private static final Logger logger = LoggerFactory.getLogger(ApplicationManagerServiceImpl.class);

	private final ProductsProvider productsProvider;
	private final RulesProvider rulesProvider;
	private final GraphicsProvider graphicsProvider;
	private final ScreenConfigurationProvider screenConfigurationProvider;
	private final ThemeProvider themeProvider;

	@Autowired
	public ApplicationManagerServiceImpl(ProductsProvider productsProvider, RulesProvider rulesProvider, GraphicsProvider graphicsProvider, ScreenConfigurationProvider screenConfigurationProvider,
			ThemeProvider themeProvider) {
		super();
		this.productsProvider = productsProvider;
		this.rulesProvider = rulesProvider;
		this.graphicsProvider = graphicsProvider;
		this.screenConfigurationProvider = screenConfigurationProvider;
		this.themeProvider = themeProvider;
	}

	@Override
	public ProductsResponse getProducts(String userName) throws Exception {
		ProductsResponse response = new ProductsResponse();
		List<Product> products = productsProvider.getProducts(userName);
		response.setProducts(products);
		return response;
	}

	@Override
	public ProductCategoriesResponse getProductCategories(String productName) {
		ProductCategoriesResponse response = new ProductCategoriesResponse();
		List<ProductCategory> productCategories = productsProvider.getProductCategories(productName);
		response.setProductCategories(productCategories);
		return response;
	}

	@Override
	public Rule getRulesByScreenName(String screenName) {
		return rulesProvider.getRulesByScreenName(screenName);
	}

	@Override
	public List<Theme> getListOfThemes() {
		return themeProvider.getThemes();
	}

	@Override
	public Theme getThemeByUUID(String themeId) {
		return themeProvider.getThemeByUUID(themeId);
	}

	@Override
	public Theme getThemeByNameVersion(String themeName, String version) {
		return themeProvider.getThemeByNameVersion(themeName, version);
	}

	@Override
	public ScreenConfiguration findConfigurationByKeyAndPath(String key, String path) throws Exception {
		return screenConfigurationProvider.findScreenConfigurationByKeyAndPath(key, path);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(ScreenConfiguration screenConfiguration, String path) throws Exception {
		screenConfigurationProvider.save(screenConfiguration, path);
	}

	@Override
	public void addImageToCollection(long screenId, long graphicsId, MultipartFile imageFile) {
		graphicsProvider.addImageToCollection(screenId, graphicsId, imageFile);
	}

	@Override
	public void updateGraphicsCollection(long screenId, List<Graphics> graphics) {
		graphicsProvider.updateGraphicsCollection(screenId, graphics);
	}
}
